/*=============================================================================
** Copyright (c) 2004~ BEA Systems, Inc.
** All rights reserved. 
**
** File Name   : eai2host.c
** Description : PGM for calling from EAI to HOST. (EAI -> HOST)
** History     : 2009.12.xx - Created by jtlee
**               
**
**===========================================================================*/

/*-----------------------------------------------------------------------------
** INCLUDE file             
**---------------------------------------------------------------------------*/
#include <stdio.h>
#include <ctype.h>
#include <atmi.h>
#include <userlog.h>

#include "../include/hostmacro.h"
#include "../include/hostglobal.h"

#define  _DBGLEVEL	100         
/**#define  _DBGLEVEL	 -1    **/ 
/** #define  _DBGLEVEL	0     **/ 

/*-----------------------------------------------------------------------------
** define & declare global variable
**---------------------------------------------------------------------------*/
extern void HexLog(char *, long);

static char *rcvbuf;
long  rcvlen;
static char *sendbuf;
long  sendlen;
MsgType		ifmsg;
long	ret_errno;

char log_msg[1024];
/*-----------------------------------------------------------------------------
** Function    : EAI2HOST
** Description : Service Main Routine 
** Buffer Type : CARRAY
** Return      : N/A
** Args        : msg - Data structure recieved from client
**---------------------------------------------------------------------------*/
#ifdef __cplusplus
extern "C"
#endif
void
#if defined(__STDC__) || defined(__cplusplus)
EAI2HOST(TPSVCINFO *msg)
#else
EAI2HOST(msg)
TPSVCINFO *msg;
#endif
{
	/*APPDBG(50 , ("Service Start...\n"));*/

	/* 
	** allocate recieve buffer 
	*/
	if (allocateBuffer() == FAIL) {
		sprintf( log_msg, "Allocate Buffer Fail");
		dmp(log_msg, 2);
		tpreturn(TPFAIL, 0, NULL, 1, 0);
	}

	/* 
	** call HOST program
	*/
	if (callHost(msg->data, msg->len) == FAIL) {
		

		/* 2010.01.26
		tpreturn(TPFAIL, 0, NULL, 1, 0);
		*/
		tpreturn(TPFAIL, ret_errno, rcvbuf, rcvlen, 0);
	}

	/*APPDBG(50 , ("Service End...\n"));*/

	/* 
	** send to client
	*/
	if (rcvlen == 0) rcvlen++;

	tpreturn(TPSUCCESS, 0, rcvbuf, rcvlen, 0); 
}

/*-----------------------------------------------------------------------------
** Function    : allocateBuffer()
** Description : allocate recieve buffer
** Return      : SUCSSES or FAIL
** Args        : N/A 
**---------------------------------------------------------------------------*/
int 
allocateBuffer()
{
	if((sendbuf = (char *) tpalloc(BUFFTYPE, NULL, BUFFLEN+1)) == NULL) {
		/*APPDBG(0 , ("tpalloc error!! tperrno=[%d] \n", tperrno));*/
		return (FAIL);
	}

	if((rcvbuf = (char *) tpalloc(BUFFTYPE, NULL, BUFFLEN+1)) == NULL) {
		/*APPDBG(0 , ("tpalloc error!! tperrno=[%d] \n", tperrno));*/

		/* 2010.02.04 */
		tpfree( sendbuf );
		return (FAIL);
	}
}

/*-----------------------------------------------------------------------------
** Function    : callHost()
** Description : call HOST program
** Return      : SUCSSES or FAIL
** Args        : N/A 
**---------------------------------------------------------------------------*/
int
callHost(data, len)
char *data;
long len;
{
	int  ret;
	char TranName[IFCodeL+1];
	char *trace_dpl=0;

	rcvlen=1;
	ret_errno = 0;
	
	/*
	** copy Interface Code (10 bytes)
	*/
	sprintf(TranName, "%-*.*s", IFCodeL, IFCodeL, data);

	/*APPDBG(0 , ("IFCode [%s] \n", TranName));*/
	
	/*
	** make send buffer (except first 8 bytes)
	*/
	memcpy(sendbuf, data+IFCodeL, len-IFCodeL);
	sendlen=len-IFCodeL;

	/*APPDBG(50 , ("SendData  Len [%d] \n", sendlen));*/

	trim(TranName);
	/* APPDBG(0 , ("IFCode [%s] \n", TranName)); */

	dmp(TranName, 1);

	trace_dpl = (char *)getenv("TRACE_DPL");
	if( trace_dpl && !strcmp(trace_dpl, TranName))
		dmp(sendbuf, 8, sendlen);

	/*HexLog(sendbuf, sendlen);*/  
	/* 
	** call HOST program
	*/
	ret = tpcall(TranName, (char *)sendbuf, sendlen, (char **)&rcvbuf, &rcvlen, (long)0);
	/* 2010.02.04 */
	ret_errno = tperrno;

	tpfree( sendbuf );
	if (ret == -1) {
		/*
		APPDBG(0 , ("callHost Fail. [%s] \n", tpstrerror(ret_errno)));
		*/

		/* 2010.01.26 */
		sprintf(rcvbuf,"call %s fail:%d:%s", TranName,ret_errno,tpstrerror(ret_errno));
		dmp(rcvbuf, 2);

		rcvlen = strlen(rcvbuf);

		return (FAIL);
	}
	/*
	APPDBG(50 , ("Receive Data Len [%d]\n", rcvlen));
	*/
	if( trace_dpl && !strcmp(trace_dpl, TranName))
		dmp(rcvbuf, 9, rcvlen);

	sprintf( log_msg, "receive data length:%d", rcvlen);
	dmp(log_msg, 2);
	dmp(rcvbuf, 3);

	/*HexLog(rcvbuf, rcvlen);*/

	return (SUCCESS);
}
/*============================<PROGRAM END>===================================*/
